package sys.database;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import java.sql.*;

public class Database {
//    final String driver = "org.hsqldb.jdbc.JDBCDriver";
//    final String url = "jdbc:hsqldb:hsql://localhost";
//    final String user = "sa";
//    final String pass = "";
    Connection con;

    // 自动装配
    @Autowired
    HikariDataSource dataSource;

    public HikariConfig config() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");
        config.setJdbcUrl("jdbc:hsqldb:hsql://localhost/");
        config.setUsername("sa");
        config.setPassword("");
        config.setMaximumPoolSize(5);
        config.setMinimumIdle(2);
        return config;
    }

    @Bean
    public HikariDataSource dataSource() {
        HikariDataSource ds = new HikariDataSource(config());
        return ds;
    }


    public Database() {
        init();
    }

    /** 连接数据库（Spring 装配 Bean 方式） */
    public void init() {
        if (con != null)
            return;
        try {
            con = dataSource.getConnection();
        } catch (SQLException e) {
            System.out.println("数据库连接失败！");
        }
    }

//    /** 连接数据库 */
//    public void init() {
//        if(con != null)
//            return;
//        try {
//            Class.forName(driver);
//            con = DriverManager.getConnection(url, user, pass);
//        } catch (ClassNotFoundException e) {
//            System.out.println("加载驱动程序失败！");
//        } catch (SQLException e) {
//            System.out.println("数据库连接失败！");
//        }
//    }

    /** 断开与数据库的连接 */
    public void close() {
        try {
            if (con != null)
                con.close();
        } catch (SQLException e) {
            System.out.println("无法断开与数据库的连接！");
        }
    }

    /** 获取Statement对象与数据库的连接 */
    public Statement createConnection() {
        try {
            return con.createStatement();
        } catch (SQLException e) {
            System.out.println("创建Statement对象失败！");
        }
        return null;
    }

    /** 获取PreparedStatement对象与数据库的连接 */
    public PreparedStatement createConnection(String sql) {
        try {
            return con.prepareStatement(sql);
        } catch (SQLException e) {
            System.out.println("创建preparedStatement对象失败！");
        }
        return null;
    }

    /** 创建表 */
    public void iniTable(String sql) {
        Statement stmt;
        stmt = createConnection();
        try {
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            System.out.println("创建数据表失败！");
        }
    }

    /**
     * 读取表
     * @param sql
     * @return
     */
    public ResultSet findTable(String sql) {
        Statement stmt = createConnection();
        try {
            return stmt.executeQuery(sql);
        } catch (SQLException e) {
            System.out.println("读取信息失败！");
        }
        return null;
    }

}
